home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / Interfaces&Libraries / Universal / Interfaces / PInterfaces / MixedMode.p < prev    next >
Encoding:
Text File  |  1998-08-17  |  12.7 KB  |  397 lines  |  [TEXT/MPS ]

  1. {
  2.      File:        MixedMode.p
  3.  
  4.      Contains:    Mixed Mode Manager Interfaces.
  5.  
  6.      Version:    Technology:    Mac OS 8
  7.                  Release:    Universal Interfaces 3.2
  8.  
  9.      Copyright:    © 1992-1998 by Apple Computer, Inc., all rights reserved.
  10.  
  11.      Bugs?:        For bug reports, consult the following page on
  12.                  the World Wide Web:
  13.  
  14.                      http://developer.apple.com/bugreporter/
  15.  
  16. }
  17. {$IFC UNDEFINED UsingIncludes}
  18. {$SETC UsingIncludes := 0}
  19. {$ENDC}
  20.  
  21. {$IFC NOT UsingIncludes}
  22.  UNIT MixedMode;
  23.  INTERFACE
  24. {$ENDC}
  25.  
  26. {$IFC UNDEFINED __MIXEDMODE__}
  27. {$SETC __MIXEDMODE__ := 1}
  28.  
  29. {$I+}
  30. {$SETC MixedModeIncludes := UsingIncludes}
  31. {$SETC UsingIncludes := 1}
  32.  
  33. {$IFC UNDEFINED __MACTYPES__}
  34. {$I MacTypes.p}
  35. {$ENDC}
  36.  
  37.  
  38. {$PUSH}
  39. {$ALIGN MAC68K}
  40. {$LibExport+}
  41.  
  42. { Mixed Mode constants }
  43. { Current Routine Descriptor Version }
  44.  
  45. CONST
  46.     kRoutineDescriptorVersion    = 7;
  47.  
  48. { MixedModeMagic Magic Cookie/Trap number }
  49.     _MixedModeMagic                = $AAFE;
  50.  
  51. { MixedModeState Version for CFM68K Mixed Mode }
  52.     kCurrentMixedModeStateRecord = 1;
  53.  
  54. { Calling Conventions }
  55.  
  56. TYPE
  57.     CallingConventionType                = UInt16;
  58.  
  59. CONST
  60.     kPascalStackBased            = 0;
  61.     kCStackBased                = 1;
  62.     kRegisterBased                = 2;
  63.     kD0DispatchedPascalStackBased = 8;
  64.     kD1DispatchedPascalStackBased = 12;
  65.     kD0DispatchedCStackBased    = 9;
  66.     kStackDispatchedPascalStackBased = 14;
  67.     kThinkCStackBased            = 5;
  68.  
  69. { ISA Types }
  70.  
  71. TYPE
  72.     ISAType                                = SInt8;
  73.  
  74. CONST
  75.     kM68kISA                    = 0;
  76.     kPowerPCISA                    = 1;
  77.  
  78. { RTA Types }
  79.  
  80. TYPE
  81.     RTAType                                = SInt8;
  82.  
  83. CONST
  84.     kOld68kRTA                    = $00;
  85.     kPowerPCRTA                    = $00;
  86.     kCFM68kRTA                    = $10;
  87.  
  88.  
  89. {$IFC TARGET_CPU_PPC }
  90.     GetCurrentISA                = kPowerPCISA;
  91.     GetCurrentRTA                = kPowerPCRTA;
  92. {$ELSEC}
  93.     {$IFC TARGET_CPU_68K }
  94.         GetCurrentISA            = kM68kISA;
  95.         {$IFC TARGET_RT_MAC_CFM }
  96.             GetCurrentRTA        = kCFM68kRTA;
  97.         {$ELSEC}
  98.             GetCurrentRTA        = kOld68kRTA;
  99.         {$ENDC}
  100.     {$ENDC}
  101. {$ENDC}
  102.  
  103. { Constants for specifing 68k registers }
  104.     kRegisterD0                    = 0;
  105.     kRegisterD1                    = 1;
  106.     kRegisterD2                    = 2;
  107.     kRegisterD3                    = 3;
  108.     kRegisterD4                    = 8;
  109.     kRegisterD5                    = 9;
  110.     kRegisterD6                    = 10;
  111.     kRegisterD7                    = 11;
  112.     kRegisterA0                    = 4;
  113.     kRegisterA1                    = 5;
  114.     kRegisterA2                    = 6;
  115.     kRegisterA3                    = 7;
  116.     kRegisterA4                    = 12;
  117.     kRegisterA5                    = 13;
  118.     kRegisterA6                    = 14;                            {  A7 is the same as the PowerPC SP  }
  119.     kCCRegisterCBit                = 16;
  120.     kCCRegisterVBit                = 17;
  121.     kCCRegisterZBit                = 18;
  122.     kCCRegisterNBit                = 19;
  123.     kCCRegisterXBit                = 20;
  124.  
  125.  
  126. TYPE
  127.     registerSelectorType                = UInt16;
  128. { SizeCodes we use everywhere }
  129.  
  130. CONST
  131.     kNoByteCode                    = 0;
  132.     kOneByteCode                = 1;
  133.     kTwoByteCode                = 2;
  134.     kFourByteCode                = 3;
  135.  
  136. { Mixed Mode Routine Records }
  137.  
  138. TYPE
  139.     ProcInfoType                        = UInt32;
  140. { Routine Flag Bits }
  141.     RoutineFlagsType                    = UInt16;
  142.  
  143. CONST
  144.     kProcDescriptorIsAbsolute    = $00;
  145.     kProcDescriptorIsRelative    = $01;
  146.  
  147.     kFragmentIsPrepared            = $00;
  148.     kFragmentNeedsPreparing        = $02;
  149.  
  150.     kUseCurrentISA                = $00;
  151.     kUseNativeISA                = $04;
  152.  
  153.     kPassSelector                = $00;
  154.     kDontPassSelector            = $08;
  155.  
  156.     kRoutineIsNotDispatchedDefaultRoutine = $00;
  157.     kRoutineIsDispatchedDefaultRoutine = $10;
  158.  
  159.     kProcDescriptorIsProcPtr    = $00;
  160.     kProcDescriptorIsIndex        = $20;
  161.  
  162.  
  163. TYPE
  164.     RoutineRecordPtr = ^RoutineRecord;
  165.     RoutineRecord = RECORD
  166.         procInfo:                ProcInfoType;                            {  calling conventions  }
  167.         reserved1:                SInt8;                                    {  Must be 0  }
  168.         ISA:                    ISAType;                                {  Instruction Set Architecture  }
  169.         routineFlags:            RoutineFlagsType;                        {  Flags for each routine  }
  170.         procDescriptor:            ProcPtr;                                {  Where is the thing we’re calling?  }
  171.         reserved2:                UInt32;                                    {  Must be 0  }
  172.         selector:                UInt32;                                    {  For dispatched routines, the selector  }
  173.     END;
  174.  
  175.     RoutineRecordHandle                    = ^RoutineRecordPtr;
  176. { Mixed Mode Routine Descriptors }
  177. { Definitions of the Routine Descriptor Flag Bits }
  178.     RDFlagsType                            = UInt8;
  179.  
  180. CONST
  181.     kSelectorsAreNotIndexable    = $00;
  182.     kSelectorsAreIndexable        = $01;
  183.  
  184. { Routine Descriptor Structure }
  185.  
  186. TYPE
  187.     RoutineDescriptorPtr = ^RoutineDescriptor;
  188.     RoutineDescriptor = PACKED RECORD
  189.         goMixedModeTrap:        UInt16;                                    {  Our A-Trap  }
  190.         version:                SInt8;                                    {  Current Routine Descriptor version  }
  191.         routineDescriptorFlags:    RDFlagsType;                            {  Routine Descriptor Flags  }
  192.         reserved1:                UInt32;                                    {  Unused, must be zero  }
  193.         reserved2:                UInt8;                                    {  Unused, must be zero  }
  194.         selectorInfo:            UInt8;                                    {  If a dispatched routine, calling convention, else 0  }
  195.         routineCount:            UInt16;                                    {  Number of routines in this RD  }
  196.         routineRecords:            ARRAY [0..0] OF RoutineRecord;            {  The individual routines  }
  197.     END;
  198.  
  199.     RoutineDescriptorHandle                = ^RoutineDescriptorPtr;
  200. { 68K MixedModeStateRecord }
  201.     MixedModeStateRecordPtr = ^MixedModeStateRecord;
  202.     MixedModeStateRecord = RECORD
  203.         state1:                    UInt32;
  204.         state2:                    UInt32;
  205.         state3:                    UInt32;
  206.         state4:                    UInt32;
  207.     END;
  208.  
  209. { Mixed Mode ProcInfos }
  210.  
  211. CONST
  212.                                                                 {  Calling Convention Offsets  }
  213.     kCallingConventionWidth        = 4;
  214.     kCallingConventionPhase        = 0;
  215.     kCallingConventionMask        = $0F;                            {  Result Offsets  }
  216.     kResultSizeWidth            = 2;
  217.     kResultSizePhase            = 4;
  218.     kResultSizeMask                = $30;                            {  Parameter offsets & widths  }
  219.     kStackParameterWidth        = 2;
  220.     kStackParameterPhase        = 6;
  221.     kStackParameterMask            = $FFFFFFC0;                    {  Register Result Location offsets & widths  }
  222.     kRegisterResultLocationWidth = 5;
  223.     kRegisterResultLocationPhase = 6;                            {  Register Parameter offsets & widths  }
  224.     kRegisterParameterWidth        = 5;
  225.     kRegisterParameterPhase        = 11;
  226.     kRegisterParameterMask        = $7FFFF800;
  227.     kRegisterParameterSizePhase    = 0;
  228.     kRegisterParameterSizeWidth    = 2;
  229.     kRegisterParameterWhichPhase = 2;
  230.     kRegisterParameterWhichWidth = 3;                            {  Dispatched Stack Routine Selector offsets & widths  }
  231.     kDispatchedSelectorSizeWidth = 2;
  232.     kDispatchedSelectorSizePhase = 6;                            {  Dispatched Stack Routine Parameter offsets  }
  233.     kDispatchedParameterPhase    = 8;                            {  Special Case offsets & widths  }
  234.     kSpecialCaseSelectorWidth    = 6;
  235.     kSpecialCaseSelectorPhase    = 4;
  236.     kSpecialCaseSelectorMask    = $03F0;
  237.  
  238.     kSpecialCase                = $000F;                        {  (CallingConventionType)  }
  239.  
  240.                                                                 {  all of the special cases enumerated.  The selector field is 6 bits wide  }
  241.     kSpecialCaseHighHook        = 0;
  242.     kSpecialCaseCaretHook        = 0;                            {  same as kSpecialCaseHighHook  }
  243.     kSpecialCaseEOLHook            = 1;
  244.     kSpecialCaseWidthHook        = 2;
  245.     kSpecialCaseTextWidthHook    = 2;                            {  same as kSpecialCaseWidthHook  }
  246.     kSpecialCaseNWidthHook        = 3;
  247.     kSpecialCaseDrawHook        = 4;
  248.     kSpecialCaseHitTestHook        = 5;
  249.     kSpecialCaseTEFindWord        = 6;
  250.     kSpecialCaseProtocolHandler    = 7;
  251.     kSpecialCaseSocketListener    = 8;
  252.     kSpecialCaseTERecalc        = 9;
  253.     kSpecialCaseTEDoText        = 10;
  254.     kSpecialCaseGNEFilterProc    = 11;
  255.     kSpecialCaseMBarHook        = 12;
  256.  
  257.  
  258. {
  259.     NOTES ON USING ROUTINE DESCRIPTOR FUNCTIONS
  260.     
  261.     When calling these routine from classic 68k code there are two possible intentions.
  262.  
  263.     The first is source compatibility with code ported to CFM (either PowerPC or 68k CFM). When
  264.     the code is compiled for CFM the functions create routine descriptors that can be used by
  265.     the mixed mode manager operating on that machine. When the code is compiled for classic 68k
  266.     these functions do nothing so that the code will run on Macintoshes that do not have a
  267.     mixed mode manager. The dual nature of these functions is achieved by turning the CFM calls
  268.     into "no-op" macros for classic 68k: You can put "NewRoutineDescriptor" in your source,
  269.     compile it for any runtime or instruction set architecture, and it will run correctly on the
  270.     intended runtime/instruction platform. All without source changes and/or conditional source.
  271.     
  272.     The other intention is for code that "knows" that it is executing as classic 68k runtime
  273.     and is specifically trying to call code of another architecture using mixed mode. Since the
  274.     routines were designed with classic <-> CFM source compatibility in mind this second case
  275.     is treated special. For classic 68k code to create routines descriptors for use by mixed mode
  276.     it must call the "Trap" versions of the routines (NewRoutineDescriptorTrap). These versions
  277.     are only available to classic 68k callers: rigging the interfaces to allow calling them
  278.     from CFM code will result in runtime failure because no shared library implements or exports
  279.     the functions.
  280.     
  281.  
  282.     This almost appears seamless until you consider "fat" routine descriptors and the advent of
  283.     CFM-68K. What does "fat" mean? CFM-68K is not emulated on PowerPC and PowerPC is not emulated
  284.     on CFM-68K. It makes no sense to create a routine descriptor having both a CFM-68K routine
  285.     and a PowerPC native routine pointer. Therefore "fat" is defined to be a mix of classic and
  286.     CFM for the hardware's native instruction set: on PowerPC fat is classic and PowerPC native,
  287.     on a 68k machine with CFM-68K installed fat is classic and CFM-68K.
  288.     
  289.     By definition fat routine descriptors are only constructed by code that is aware of the 
  290.     architecture it is executing as and that another architecture exists. Source compatibility
  291.     between code intented as pure classic and pure CFM is not an issue and so NewFatRoutineDescriptor
  292.     is not available when building pure classic code.
  293.     
  294.     NewFatRoutineDescriptorTrap is available to classic code on both PowerPC and CFM-68K. The
  295.     classic code can use the code fragment manager routine "FindSymbol" to obtain the address of 
  296.     a routine in a shared library and then construct a routine descriptor with both the CFM routine 
  297.     and classic    routine.
  298. }
  299.  
  300. {$IFC TARGET_OS_MAC AND TARGET_RT_MAC_CFM }
  301. FUNCTION NewRoutineDescriptor(theProc: ProcPtr; theProcInfo: ProcInfoType; theISA: ISAType): UniversalProcPtr;
  302. PROCEDURE DisposeRoutineDescriptor(theProcPtr: UniversalProcPtr);
  303. FUNCTION NewFatRoutineDescriptor(theM68kProc: ProcPtr; thePowerPCProc: ProcPtr; theProcInfo: ProcInfoType): UniversalProcPtr;
  304. {$ELSEC}
  305. { Note that the call to NewFatRoutineDescriptor is undefined. }
  306. FUNCTION NewRoutineDescriptor(theProc: ProcPtr; theProcInfo: ProcInfoType; theISA: ISAType): UniversalProcPtr;
  307.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  308.     INLINE $5C4F, $2E9F;
  309.     {$ENDC}
  310. PROCEDURE DisposeRoutineDescriptor(theProcPtr: UniversalProcPtr);
  311.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  312.     INLINE $584F;
  313.     {$ENDC}
  314. {$ENDC}
  315.  
  316.  
  317. {$IFC TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM }
  318. {
  319.     The "Trap" versions of the MixedMode calls are only for classic 68K clients that 
  320.     want to load and use CFM based code.
  321. }
  322. FUNCTION NewRoutineDescriptorTrap(theProc: ProcPtr; theProcInfo: ProcInfoType; theISA: ISAType): UniversalProcPtr;
  323.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  324.     INLINE $7000, $AA59;
  325.     {$ENDC}
  326. PROCEDURE DisposeRoutineDescriptorTrap(theProcPtr: UniversalProcPtr);
  327.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  328.     INLINE $7001, $AA59;
  329.     {$ENDC}
  330. FUNCTION NewFatRoutineDescriptorTrap(theM68kProc: ProcPtr; thePowerPCProc: ProcPtr; theProcInfo: ProcInfoType): UniversalProcPtr;
  331.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  332.     INLINE $7002, $AA59;
  333.     {$ENDC}
  334. {$ENDC}
  335.  
  336. {$IFC TARGET_RT_MAC_CFM }
  337. {
  338.     CallUniversalProc is only implemented in shared libraries on CFM68K and PowerPC, it is now
  339.     conditionalize with TARGET_RT_MAC_CFM.  This will catch accidental calls from classic 68K code
  340.     that previously only showed up as linker errors.
  341. }
  342. FUNCTION CallUniversalProc(theProcPtr: UniversalProcPtr; procInfo: ProcInfoType; ...): LONGINT; C;
  343. FUNCTION CallOSTrapUniversalProc(theProcPtr: UniversalProcPtr; procInfo: ProcInfoType; ...): LONGINT; C;
  344. {$ENDC}  {TARGET_RT_MAC_CFM}
  345.  
  346. {$IFC TARGET_CPU_68K }
  347. FUNCTION SaveMixedModeState(VAR stateStorage: MixedModeStateRecord; stateVersion: UInt32): OSErr;
  348.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  349.     INLINE $7003, $AA59;
  350.     {$ENDC}
  351. FUNCTION RestoreMixedModeState(VAR stateStorage: MixedModeStateRecord; stateVersion: UInt32): OSErr;
  352.     {$IFC TARGET_OS_MAC AND TARGET_CPU_68K AND NOT TARGET_RT_MAC_CFM}
  353.     INLINE $7004, $AA59;
  354.     {$ENDC}
  355. {$ENDC}  {TARGET_CPU_68K}
  356.  
  357. { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
  358.  *
  359.  *    Macros for building ProcInfos.  Examples:
  360.  *    
  361.  *    
  362.  *    uppModalFilterProcInfo = kPascalStackBased
  363.  *         | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
  364.  *         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(DialogPtr)))
  365.  *         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(EventRecord*)))
  366.  *         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(short*))),
  367.  *
  368.  *    uppDeskHookProcInfo = kRegisterBased
  369.  *         | REGISTER_ROUTINE_PARAMETER(1, kRegisterD0, SIZE_CODE(sizeof(Boolean)))
  370.  *         | REGISTER_ROUTINE_PARAMETER(2, kRegisterA0, SIZE_CODE(sizeof(EventRecord*)))
  371.  *
  372.  *    uppGXSpoolResourceProcInfo = kCStackBased
  373.  *         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  374.  *         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(gxSpoolFile)))
  375.  *         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(Handle)))
  376.  *         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(ResType)))
  377.  *         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
  378.  *
  379.  *    uppTEFindWordProcInfo = SPECIAL_CASE_PROCINFO( 6 ),
  380.  *
  381.  }
  382.  
  383.  
  384.  
  385.  
  386.  
  387. {$ALIGN RESET}
  388. {$POP}
  389.  
  390. {$SETC UsingIncludes := MixedModeIncludes}
  391.  
  392. {$ENDC} {__MIXEDMODE__}
  393.  
  394. {$IFC NOT UsingIncludes}
  395.  END.
  396. {$ENDC}
  397.